package com.lily;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author lily
 */
public class testRedis {
    private Jedis jedis=null;
    @Before
    public void init(){
        jedis = new Jedis("127.0.0.1", 6379);
    }

    @Test
    public void testString(){
        jedis.set("name","lily");

        String name = jedis.get("name");
        System.out.println(name);

        jedis.del("name");
        System.out.println(jedis.get("name"));

    }

    @Test
    public void testHash(){
        jedis.hset("myHash","h1","v1");

        String h1 = jedis.hget("myHash","h1");


        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("key1", "val1");
        hashMap.put("key2", "val2");
        hashMap.put("key3", "val3");
        jedis.hmset("myHash",hashMap);

        Map<String, String> map = jedis.hgetAll("myHash");
        for (String key:map.keySet()) {
            System.out.println(key+"==="+map.get(key));
        }
        System.out.println("//////////////////////////////////");

        jedis.hdel("myHash","key2");
        map = jedis.hgetAll("myHash");
        for (String key:map.keySet()) {
            System.out.println(key+"==="+map.get(key));
        }

        jedis.del("myHash");
    }

    @Test
    public void testList() {
        //2.操作redis数据库
        jedis.lpush("mylist", "张三", "李四", "王五");

        List<String> mylist = jedis.lrange("mylist", 0, -1);
        System.out.println(mylist);
    }

    @Test
    public void testSet() {
        //2.操作redis数据库
        jedis.sadd("myset", "张三", "李四", "王五");

        Set<String> myset = jedis.smembers("myset");
        System.out.println(myset);
    }



    @After
    public void destroy(){
        //3.释放资源
        jedis.close();
    }
}
